home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / display / util / findjpg.c next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  1.2 KB  |  65 lines

  1. /*
  2.   Find JFIF header in image
  3.   
  4.   Written by Jih-Shin Ho, 1994
  5.   Copyright 1994 by Jih-Shin Ho
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. static int match_count;
  11. static int num_id = 4;
  12. static unsigned char id[] = {0xff,0xd8,0xff,0xe0};
  13.  
  14.  
  15. main(int argc,char *argv[])
  16. {
  17.   int i,found;
  18.   FILE *in,*out;
  19.  
  20.   if (argc != 3) {
  21.     printf("\nUsage: findjpg input_file output_file\n\n");
  22.     exit(0);
  23.   }
  24.   in = fopen(argv[1],"rb");
  25.   if (in == NULL) {
  26.     printf("Can't open %s\n",argv[1]);
  27.     exit(1);
  28.   }
  29.   out = fopen(argv[2],"wb");
  30.   if (out == NULL) {
  31.     fclose(in);
  32.     printf("Can't open %s\n",argv[2]);
  33.     exit(1);
  34.   }
  35.  
  36.   match_count = found = 0;
  37.   while ((i = getc(in)) != EOF) {
  38.     if (i == id[match_count]) {
  39.       match_count++;
  40.       if (match_count == num_id) {
  41.         printf("JFIF header found.\n");
  42.         found = 1;
  43.         break;
  44.       }
  45.     }
  46.     else if (match_count) {
  47.       fseek(in,-match_count,SEEK_CUR);
  48.       match_count = 0;
  49.     }
  50.   }
  51.   if (found) {
  52.     for (i = 0; i < num_id; ++i) putc(id[i],out);
  53.     while ((i = getc(in)) != EOF) putc(i,out);
  54.   }
  55.   fclose(in);
  56.   fclose(out);
  57.   if (found) printf("\nDone.\n\n");
  58.   else {
  59.     remove(argv[2]);
  60.     printf("\nJFIF not found.\n\n");
  61.   }
  62.   return(0);
  63. }
  64.  
  65.